package com.telenav.xmpp;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;

import com.telenav.framework.network.HostProvider;

public class XmppTool {

	private static XMPPConnection con = null;

	private static void openConnection() {
		try {
			ConnectionConfiguration connConfig = new ConnectionConfiguration(
					HostProvider.XMPP_SERVER_URL, 5222);
			connConfig.setSASLAuthenticationEnabled(false);

			con = new XMPPConnection(connConfig);

			PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
			con.addPacketListener(new MassgePacketListener(), filter);
			con.connect();
		} catch (XMPPException xe) {
			xe.printStackTrace();
		}
	}

	public static XMPPConnection getConnection() {
		if (con == null) {
			openConnection();
		}
		else
		{
			try {
				if(!con.isConnected())
				{
					con.connect();
				}	
			} catch (XMPPException e) {

				e.printStackTrace();
			}
		}
		return con;
	}

	public static void closeConnection() {
		con.disconnect();
		con = null;
	}
}
